package youcaihuaoa.weetop.com.ui.main

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarView
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.SignGroupBean
import com.robot.base.util.LiveDataBus
import kotlinx.android.synthetic.main.app_activity_sign.*
import youcaihuaoa.weetop.com.BR
import youcaihuaoa.weetop.com.R
import youcaihuaoa.weetop.com.databinding.AppActivityBasicInfoBinding
import youcaihuaoa.weetop.com.databinding.AppActivitySignBinding
import youcaihuaoa.weetop.com.vm.main.BasicInfoViewModel
import youcaihuaoa.weetop.com.vm.main.SignViewModel

@Route(path = AppConstants.Router.Main.A_SIGN)
class SignActivity : GLBaseActivity<AppActivitySignBinding, SignViewModel>() {


    override fun initComponents() {

        binding.calendarView.setOnMonthChangeListener { year, month ->
            binding.tvTitle.text = "${year}年${month}月"
            viewModel.fetchSignConfig(
                "${year}-${month}-01"
            )
        }
        binding.calendarView.setOnCalendarSelectListener(object :
            CalendarView.OnCalendarSelectListener {
            override fun onCalendarOutOfRange(calendar: Calendar?) {

            }

            override fun onCalendarSelect(calendar: Calendar?, isClick: Boolean) {
                LogUtils.e(calendar)
                viewModel.fetchSignConfig(
                    "${calendar?.year}-${calendar?.month}-${
                        if ((calendar?.day?:0) < 10) {
                            "0${calendar?.day}"
                        }else{
                            calendar?.day
                        }
                    }"
                )
            }

        })
        val month = binding.calendarView.curMonth
        val year = binding.calendarView.curYear
        binding.tvTitle.text = "${year}年${month}月"
        viewModel.fetchSignConfig(TimeUtils.millis2String(System.currentTimeMillis(), "yyyy-MM-dd"))
    }

    override fun initViewObservable() {
        LiveDataBus.get().with("sign_success").observe(this) {
            viewModel.fetchSignConfig(
                TimeUtils.millis2String(
                    System.currentTimeMillis(),
                    "yyyy-MM-dd"
                )
            )
        }
    }


    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_sign

    override fun initVariableId(): Int = BR.viewModel
}